package org.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.demo.entity.UserMeeting;
import org.example.demo.mapper.UserMeetingMapper;
import org.example.demo.service.UserMeetingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户会议关联服务实现类
 */
@Service
public class UserMeetingServiceImpl extends ServiceImpl<UserMeetingMapper, UserMeeting> implements UserMeetingService {

    @Override
    public Page<UserMeeting> selectPage(UserMeeting userMeeting, Integer pageNum, Integer pageSize) {
        Page<UserMeeting> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        QueryWrapper<UserMeeting> queryWrapper = new QueryWrapper<>();

        // 构建查询条件
        queryWrapper.lambda()
                .eq(userMeeting.getUserId() != null, UserMeeting::getUserId, userMeeting.getUserId())
                .eq(userMeeting.getMeetingId() != null, UserMeeting::getMeetingId, userMeeting.getMeetingId());

        return this.page(page, queryWrapper);
    }

    @Override
    public List<Long> getMeetingIdsByUserId(Long userId) {
        LambdaQueryWrapper<UserMeeting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserMeeting::getUserId, userId);

        return this.list(queryWrapper).stream()
                .map(UserMeeting::getMeetingId)
                .collect(Collectors.toList());
    }

    @Override
    public List<Long> getUserIdsByMeetingId(Long meetingId) {
        LambdaQueryWrapper<UserMeeting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserMeeting::getMeetingId, meetingId);

        return this.list(queryWrapper).stream()
                .map(UserMeeting::getUserId)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(Long meetingId, List<Long> userIds) {
        // 先删除该会议下的所有关联
        LambdaQueryWrapper<UserMeeting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserMeeting::getMeetingId, meetingId);
        this.remove(queryWrapper);

        // 批量添加新的关联
        if (userIds != null && !userIds.isEmpty()) {
            List<UserMeeting> userMeetings = new ArrayList<>();
            userIds.forEach(userId -> {
                UserMeeting userMeeting = new UserMeeting();
                userMeeting.setMeetingId(meetingId);
                userMeeting.setUserId(userId);
                userMeetings.add(userMeeting);
            });
            return this.saveBatch(userMeetings);
        }
        return true;
    }

    @Override
    public boolean remove(Long meetingId, Long userId) {
        LambdaQueryWrapper<UserMeeting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserMeeting::getMeetingId, meetingId)
                .eq(UserMeeting::getUserId, userId);
        return this.remove(queryWrapper);
    }
}